import { PermissionsAndroid, Platform } from 'react-native';
import AndroidModule from './AndroidConnent'

export function requestWritePermission() {

    return new Promise(async (resolve, reject) => {
        try {
            const permissions = [
                PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
                PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE
            ]
            //返回得是对象类型
            const granteds = await PermissionsAndroid.requestMultiple(permissions)
        
            if (granteds["android.permission.WRITE_EXTERNAL_STORAGE"] === "granted") {
                resolve(true)
            } else {
                resolve(false)
            }
        } catch (err) {
        }
    }
    )  

}

export function requestCameraPermission() {

    return new Promise(async (resolve, reject) => {
        try {
            const permissions = [
                PermissionsAndroid.PERMISSIONS.CAMERA
            ]
            //返回得是对象类型
            // const granteds = await PermissionsAndroid.requestMultiple(permissions)

            PermissionsAndroid.check('android.permission.CAMERA').then(res=>{
                if(!res){
                    // Alert.alert(
                    //     "相机、存储",
                    //     "相机用于上传头像、上传行驶证正本和扫一扫等用于捕获图像的场景。存储用于app保存图片/文件等信息",
                    //     [
                    //         {
                    //             text: "取消",
                    //             style: "cancel",
                    //         },
                    //         {
                    //             text: "确定",
                    //             onPress: async ()=>{
                    //                 const granteds = await PermissionsAndroid.requestMultiple(permissions)
                    //                 if (granteds["android.permission.CAMERA"] === "granted") {
                    //                     printlog('同意拍照权限')
                    //                     resolve(true)
                    //                 } else {
                    //                     printlog('不同意拍照权限')
                    //                     resolve(false)
                    //                 }
                    //             }
                    //         }
                    //     ],
                    // )
                    resolve(false)
                }
                else{
                    resolve(true)
                }
            }) 
        
            // if (granteds["android.permission.CAMERA"] === "granted") {
            //     resolve(true)
            // } else {
            //     resolve(false)
            // }
        } catch (err) {
        }
    }
    )

}


export function requestCameraAndWritePermission() {

    return new Promise(async (resolve, reject) => {
        try {
            const permissions = [
                PermissionsAndroid.PERMISSIONS.CAMERA
            ]

            PermissionsAndroid.check('android.permission.CAMERA' | 'android.permission.WRITE_EXTERNAL_STORAGE').then(res=>{
                if(!res){
                    
                    resolve(false)
                }
                else{
                    resolve(true)
                }
            }) 
        
            
        } catch (err) {
        }
    }
    )

}

export function requestLocationPermission() {

    return new Promise(async (resolve, reject) => {
        try {
           const permissions = [
                    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
                ]
                //返回得是对象类型
                const granteds = await PermissionsAndroid.requestMultiple(permissions)
                if (granteds["android.permission.ACCESS_FINE_LOCATION"] === "granted") {
                    // geolocationInit();
                    // getCurrentPosition().then((datajd) => {
                    //     let coords = datajd.coords;
                    //     printlog(coords.latitude + '    ' + coords.longitude)
                    //     getAddress(coords.latitude, coords.longitude)
                    // }).catch(function (error) {
                    // });
                    resolve(true)
                } else {
                    resolve(false)
                }

        } catch (err) {
        }
    })

}

